package com.yjd.bi.report.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yjd.bi.base.service.impl.BaseServiceImpl;
import com.yjd.bi.report.dao.IBiEtlFromtableTotableDao;
import com.yjd.bi.report.model.BiEtlFromtableTotableModel;
import com.yjd.bi.report.service.IBiEtlFromtableTotableService;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.util.FrameUtil;

/**
 * <b>description</b>：数据抽取中原表和目标表关联信息业务实现<br>
 * <b>time</b>：2016-04-08 15:29:37 <br>
 * <b>author</b>： ready likun_557@163.com
 */
@Service("biEtlFromtableTotableService")
public class BiEtlFromtableTotableServiceImpl extends BaseServiceImpl implements
		IBiEtlFromtableTotableService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public BiEtlFromtableTotableModel insert(BiEtlFromtableTotableModel model)
			throws Exception {
		this.disposeBeforeSave(model);
		return this.biEtlFromtableTotableDao.insertModel(model, true);
	}

	public void disposeBeforeSave(BiEtlFromtableTotableModel model)
			throws Exception {
		if (model == null || model.getFrom_table_id() == null
				|| model.getTo_table_id() == null) {
			FrameUtil.throwBaseException1("来源表、目标表不能为空");
		}
		BiEtlFromtableTotableModel tm = this.biEtlFromtableTotableDao
				.getModelOne(
						FrameUtil.newHashMap("from_table_id",
								model.getFrom_table_id(), "to_table_id",
								model.getTo_table_id()), true);
		if ((model.getId() != null && !model.getId().equals(tm.getId()))
				|| (model.getId() == null && tm != null)) {
			FrameUtil.throwBaseException1("该配置已存在，请不要重复配置!");
		}
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(BiEtlFromtableTotableModel model) throws Exception {
		this.disposeBeforeSave(model);
		model.setUpdate_time(FrameUtil.getTime());
		return this.biEtlFromtableTotableDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.biEtlFromtableTotableDao.deleteModel(paramMap, true);
	}

	@Resource
	private IBiEtlFromtableTotableDao biEtlFromtableTotableDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.biEtlFromtableTotableDao;
	}

}
